package tgc.edu.zz.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.zz.entity.Menu;
import tgc.edu.zz.entity.TreeTableModel;
import tgc.edu.zz.form.MenuForm;
import tgc.edu.zz.service.MenuService;

@Controller
@RequestMapping("/menu")
public class MenuController {

	@Autowired
	private MenuService menuService;
	
	@RequestMapping("/manage")
	public String page() {
		return "menu/manage";
	}
	@RequestMapping(value="/treePage")
	@ResponseBody
	public Object treePage(MenuForm form) {
		Sort sort=Sort.by("idx1");
		List<Menu> list = menuService.findAll(sort);
		return buildAllTree(list);
	}
	private List<HashMap<String, Object>> buildAllTree(List<Menu> list) {
		List<HashMap<String, Object>> result=new ArrayList<>();
		for(Menu menu:list) {
			HashMap<String, Object> node=new HashMap<>();
			node.put("id", menu.getId());
			node.put("name",menu.getName());
			node.put("url", menu.getUrl());
			node.put("icon", menu.getIcon());
			node.put("children", buildAllTree(menu.getChildren()));
			result.add(node);
		}
		return result;
	}
	
//	private List<HashMap<String, Object>> buildAllTree(List<Menu> list, Integer id) {
//		List<HashMap<String, Object>> result=new ArrayList<>();
//		for(Menu menu:list) {
//			if(menu.getId()!=id) {
//				HashMap<String, Object> node=new HashMap<>();
//				node.put("id", menu.getId()==null?"":menu.getId());
//				node.put("name", menu.getName()==null?"":menu.getName());
//				node.put("pid", menu.getParent());
//				node.put("nodes", buildAllTree(menu.getChildren(), id));
//				result.add(node);
//			}
//		}
//		return result;
//	}
	
}
